侯捷c++11新特性
# 特性1:Variadic Templates 可变参数模板
// ...参数个数可变,参数类型可变
// sizeof...(args) 返回函数参数的数目
// sizeof...(Types) 返回类型参数的数目
// 可以很方便完成recursive function call
void print(){}//个数为0时调用,相当于终止点
template<typename T, typename... Types>
void print(const T& firstArg, const Types&... args)
{
cout << firstArg << endl;
print(args...);
}
//与上面的函数可以并存,上面的是特化版本
template<typename... Types>
void print(const Types&... args){}
// 可以完成recursive inheritance
template<typename... Valuse> class tuple;
template<> class tuple<>{};
template<typename Head, typename... Tail>
class tuple<Head, Tail...>:private tuple<Tail...>{};
// recursive composition
template<typename... Values> class tup;
template<> class tup<>{};
template<typename Head, typename... Tail>
class tup<Head, Tail...>
{
typedef tup<Tail...> composited;
protected:
composited m_tail;
Head m_head;
public:
}
# 特性2:空指针nullptr
对指针初始化为空时,赋值为nullptr
# 特性3:initializer list
int j{};//0
int* p{};//nullptr
int i{5.3};//warning:initialer list not allow narrowing
void print(std::initializer_list<int> vals)
{
for(auto p=vals.begin(); p!=vals.end(); ++p){
std::cout<<*p<<"\n";
}
}
# 特性4:explict
explicit 用于转换convert的时候,明确的,不能转换
# 特性5:=default, =delete
Foo(const Foo&) copy ctor cannot be overload
//当自定义了拷贝构造函数,默认的拷贝构造函数无法=default或=delete
Foo& operator(const Foo&) operator= cannot be overload(copy assign)
//当自定义了拷贝赋值函数,默认的拷贝赋值函数无法=default或=delete
# 特性6:alias template(模板的别名)
template<typename T>
using Vec = std::vector<T, MyAlloc<T>>;
Vec<int> is equivalent to std::vector<int, MyAlloc<int>>
template template parameter
template<typename name, template<class> class Container>
class XCls{};
//Error: vector need two template parameter
XCls<MyString, vector> cl;
//以下版本是对的
template<typename T>
using Vec = vector<T, alloctor<T>>;
template<typename name, template<class> class Container>
class XCls{};
XCls<MyString, Vec> cl;
# 特性7:type alias
//typedef void(*func)(int, int);
using func = void(*)(int, int);
# 特性8:noexcept
//用来指明某个函数无法——或不打算——抛出异常。
void foo() noexcept;
//若有异常未在foo()内被处理,亦即如果foo()抛出异常,程序会被终止。
# 特性9:override final
virtual void func(int);//A
virtual void func(int) override;//B:A 告诉编译器B重写了A中的func函数
class A final{};//A不允许被继承
class A{
vitural void f() final;//f函数不允许被重写
}
# 特性10:decltype
let the compiler find out the type of an expression
map<string,float> coll;
decltype(coll)::value_type elem;
应用1:声明返回类型
template<typename T1, typename T2>
decltype(x+y) add(T1 x, T2 y);//编译不通过
template<typename T1, typename T2>
auto add(T1 x, T2 y) -> decltype(x+y);//this uses the same syntax as for lambdas to declare return types.
应用2:超编程 metaprogramming
typedef typename decltype(obj)::iterator iType;
应用3:传递一个lambda类型 used to pass the type of a lambda
auto cmp = [](const Person& p1, const Person& p2){
return p1.lastname()<p2.lastname() ||
(p1.lastname()==p2.lastname() && p1.firstname()<p2.firstname());
};
std::set<Person,decltype(cmp)> coll(cmp);
# 特性11:Lambda
[]() mutable -> retType{};
[=] pass by value
[&] pass by reference
mutable, operator()被定义为non-const成员函数
# 特性12:Revalue references
solve the problem of unneccessary copying and enable perfect forwarding when the right-hand side of an assignment is an rvalue, then the left-hand side object can steal resources from the right-hand side object rather than performing a separate allocation Lvalue:可以出现在operate=左侧 Rvalue:只能出现在operate=右侧
# 特性13:hashtable
当元素个数大于bucket vector大小时,会rehashing,增长到原来bucket vector的二倍